package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.COGS;
import com.eece496.Database.StudentGroups;
import com.eece496.Helpers.COGSInfo;

/**
 * Servlet implementation class ProfTADutyServlet
 */
public class ProfTADutyServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfTADutyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connect();
		
		String courseNumber = request.getParameter("courseNumber");
		String sectionNumber = request.getParameter("sectionNumber");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		
		COGS COGSDB = new COGS();
		COGSDB.Setup(connection);
		
		ArrayList<COGSInfo> info = new ArrayList<COGSInfo>();
		ArrayList<Integer> numOfTAsNeeded = new ArrayList<Integer>();
		try {
			info = COGSDB.GetCOGSList(courseNumber, sectionNumber);
			
			StudentGroups groupDB = new StudentGroups();
			groupDB.Setup(connection);
			for(COGSInfo cinfo: info)
			{
				int size = groupDB.getNumberOfGroupsInDivision(courseNumber, sectionNumber, cinfo.division);
				size = size / 2;
				numOfTAsNeeded.add(size);
			}
			request.setAttribute("numOfTAsNeeded", numOfTAsNeeded);
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		request.setAttribute("COGSInfo", info);
		request.setAttribute("courseNumber", courseNumber);
		request.setAttribute("sectionNumber", sectionNumber);
		request.setAttribute("firstname", firstname);
		request.setAttribute("lastname", lastname);
		
		request.getRequestDispatcher("/Professor/Professor-TA-Duties.jsp")
		.forward(request, response);
	}

}
